const API = require('../../pages/api/index')
const {
  typeJson
} = require('../../utils/typejson')
let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
import {
  get,
  post
} from '../../request/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    name: '领导带班',
    dialogShow: false,
    activeTtem: {},
    signPic: '', // 签名图片
    dataList: [],
    pulldown: "",
    pullup: 2,
    addButton: true,
    Refresh: false,
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    listConfig: [{
      label: '带班日期',
      prop: 'startTime'
    },
    {
      label: '值班内容',
      prop: 'checkRecord',
    }
    ],

    slide: {
      slideButton: {
        text: '编辑',
        extClass: 'test',
        src: '/page/weui/cell/icon_star.svg', // icon的路径
      },
      formConfig: [{
        label: '带班领导',
        prop: 'leaderId',
        hidden: "all",
        tag: "userid"
      },

      {
        label: '带班日期',
        prop: 'leaderDate',
        type: "dateTime",
        required: true,
        hidden: 'look',
      },
      {
        label: '带班日期',
        prop: 'meetingDate',
        hidden: 'edit',
      },
      {
        label: '班中巡查',
        prop: 'examine',
      },
      {
        label: "总结记录",
        prop: "remark",
        type: "textarea",
      },
      {
        label: '交接班记录',
        prop: 'changeWorkRec',
        type: "textarea",
      },
      {
        label: '上传照片',
        prop: 'files',
        type: 'image',

      },
      ],
    },

    formConfig: [{
      label: '值班名称',
      prop: 'planName',
    },
    {
      label: '带班开始时间',
      prop: 'createTime',
      type: "dateTime",

    },

    {
      label: '带班结束时间',
      prop: 'endTime',
      type: "dateTime",
    },
    {
      label: '计划值班人员',
      prop: 'checkPersonName',
      type: 'multiDropDown',
      required: true,
      hidden: 'look',
      tag: 'userid',
      net: {
        url: '/system/user/list?pageNum=1&pageSize=999',
        method: 'get',
        key: 'memberId',
        value: 'nickName'
      }
    },
    {
      label: ' 签到值班人员',
      type: "List",
      prop: 'finalAttendPerson',
    },
    {
      label: "创建时间",
      prop: "createTime"
    },
    {
      label: "值班内容",
      prop: "checkRecord"
    },
    ],


    net: {

      select: {
        url: `/app/inspection/list`,
        method: 'get',
        data: {
          mineId: true,
          userId: "userid",

        }
      },
      delete: {
        url: '/app/siteLeaderrec/delete',
        method: 'post',
        data: {

        }
      },
      insert: {
        url: '/app/siteLeaderrec/edit',
        method: 'post',
        data: {
          mineId: true,
          leaderId: "userid",

        }
      },
      update: {
        url: '/app/siteLeaderrec/edit',
        method: 'post',
        data: {

        }
      }


    },
    showCom: true
  },
  dutyLogs(e) {
    let parems = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: `/pages/dutyLogs/index?info=${parems}`,
    })
  },
  addButtonTap() {
    wx.navigateTo({
      url: `/pages/leaderDutyadd/index?type=${this.data.type}`,
    })
  },
  // 签到
  sign(e) {
    let data = e.currentTarget.dataset.item
    let time = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0, 8)
    let parmes = {
      planId: data.id,
      checkPart:0,
      userId: this.data.prolist.userId,
      registTime: time,
      status: 1
    }
    post('/app/sitedetail/add', parmes).then(res => {
      if (res.code == 200) {
        this.getList()
        wx.showToast({
          title: '提交成功',
          duration: 3000,
          icon: 'success',
          mask: true
        })
      }
    })
  },
  // 下班
  downWork(e) {
    console.log(e.currentTarget.dataset.item);
    this.setData({
      activeTtem: e.currentTarget.dataset.item,
      dialogShow: true
    })
  },
  dialogClose() {
    this.setData({
      activeTtem: {},
      dialogShow: false,
      signPic: ''
    })
  },
  // 签名
  canvasChang(e) {
    this.setData({
      signPic: e.detail
    })
  },
  // 提交签名信息
  dialogConfirm() {
    post('/app/sitelog/getAlldata', {
      userId: this.data.prolist.userId,
      planId: this.data.activeTtem.id,
      siteType: 1
    }).then(res => {
      let id = null
      let val = res.rows
      for (let index = 0; index < val.length; index++) {
        if (val[index].checkPart == 0) {
          id = val[index].id;
        }
      }
      console.log(id);
      let obj = {
        checkPart: 0,
        signPic: this.data.signPic,
        userId: this.data.prolist.userId,
        planId: this.data.activeTtem.id,
      }
      post('/app/sitedetail/edit', obj).then(res => {
        if (res.code == 200) {
          this.getList()
          wx.showToast({
            title: '提交成功',
            duration: 3000,
            icon: 'success',
            mask: true
          })

        }
      })
    })


    // let parmes = {
    // this.data.activeTtem 选中记录
    // this.data.signPic
    // userId: this.data.prolist.userId,用户id
    // }
    this.dialogClose() // 提交成功清除记录,关闭弹框
  },
  // 提交报告
  submitWork(e) {
    let parems = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: `/pages/workReport/index?info=${parems}`,
    })
  },
  async getList() {
    const userInfo = JSON.parse(wx.getStorageSync('prolist'))
    let parmes = {
      userid: userInfo.userId,
      mineId: 1,
      type: this.data.type,
    }
    let res = await get('/app/inspection/list' + "?currentPage=1&pageSize=10", parmes)
    this.setData({
      dataList: res.rows
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    this.setData({
      type: option.type,
      name: option.type == 1 ? '领导带班' : '部门值班'
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      showCom: false
    });
    this.setData({
      showCom: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      Refresh: true
    })
    wx.hideLoading();
    wx.stopPullDownRefresh()
    this.setData({
      Refresh: false
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pullup: this.data.pullup + 1
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})